



//constructor 
function AdminPublic(variable){
    this.windows = new Array();
    this.msgs=new Array();
    this.msgTimer="";
    this.membersTimer="";
    this.referenceExcl=variable;
    this.receiver="";
    this.startTimers();
}

AdminPublic.prototype.startTimers=function()
{
	this.msgTimer = window.setInterval(this.referenceExcl+".updateUser()",3000);
	this.membersTimer = window.setInterval(this.referenceExcl+".updateMembers()",10000);
} 



AdminPublic.prototype.stopTimers=function()
{
    window.clearInterval (this.msgTimer);
    window.clearInterval (this.membersTimer);
    this.msgTimer="";
    this.membersTimer="";
}
AdminPublic.prototype.updateUser=function()
{
    PageMethods.UpdateUser($get("hdnRoomID").value, this.UpdateMessages);
}
AdminPublic.prototype.updateMembers=function()
{
    PageMethods.UpdateRoomMembers($get("hdnRoomID").value, this.UpdateMembersList);
}
AdminPublic.prototype.Leave=function ()
{
    try{
    this.stopTimers();
    PageMethods.LeaveRoom($get("hdnRoomID").value);
    }catch(e){}
}

AdminPublic.prototype.button_clicked=function ()
{
	PageMethods.SendMessage($get("txtMsg").value,$get("hdnRoomID").value,$get("hdnReceiver").value,this.UpdateMessages, this.errorCallback);
	$get("txtMsg").value="";	
}
AdminPublic.prototype.clickButton=function(e, buttonid)
{ 
	var bt = document.getElementById(buttonid); 
	if (typeof bt == 'object')
		{ 
			if(navigator.appName.indexOf("Netscape")>(-1))
			{ 
			    if (e.keyCode == 13)
			    { 
				    bt.click(); 
				    return false; 
			    } 
		    } 
	    if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1))
		    { 
			    if (event.keyCode == 13)
			    { 
			        bt.click(); 
				    return false; 
			    } 
		    } 
	    }	 
} 


AdminPublic.prototype.ParserMsgLine=function(msg){
    if (msg && msg.Msg!=""){
        var currentUser=$get("hdnCurrentAccount").value;
        var color=(currentUser==msg.Sender)?"black":"Blue";
        var str="<span style='color:{0}'><b style='font-size:13px;'>{1}:</b> {2}</span><br/>";        
        return StringUtils.format(str,color,msg.Sender,msg.Msg);    
    }else return "";
}
		
AdminPublic.prototype.UpdateMessages=function (result)
{
    var display=$get("displayMessage");
    APublic.msgs=APublic.msgs.concat(result);
    result=APublic.msgs;
    if (result){        
        var currentAccount=$get("hdnCurrentAccount").value;
        for (var i=0;i< result.length;i++){     
            try{
            var msg=result[i];
          
            if (msg.Sender!=currentAccount &&  ((APublic.windows[msg.Sender]==null ) || 
                (APublic.windows[msg.Sender]!=null && APublic.windows[msg.Sender].document==null))){
                
                if (msg.Type!=3){
                APublic.windows[msg.Sender]=APublic.openWindow("Private.aspx?rid="+$get("hdnRoomID").value+"&receiver="+msg.Sender,msg.Sender);
                }               
            }            
          
          
          if (APublic.windows[msg.Sender]){             
            if (APublic.windows[msg.Sender].document!=null ){
                display=APublic.windows[msg.Sender].document.getElementById("displayMessage");     
            }                   
          }else
            if (APublic.windows[msg.Receiver]){             
                if (APublic.windows[msg.Receiver].document!=null ){
                    display=APublic.windows[msg.Receiver].document.getElementById("displayMessage");       
                }   
            }         
         
          display.innerHTML=display.innerHTML+APublic.ParserMsgLine(msg);
          APublic.msgs.shift( );
          display.scrollTop=display.scrollHeight-display.offsetHeight;  
          }catch(e){}                        
       }
    }
    
}

AdminPublic.prototype.UpdateMembersList=function(result)
{
   // alert(result);
    var users=result.split(",");
   // alert(users.length);
    var i=0;
    
    
    $get("lstMembers").options.length=0;
     var i=0;
    while (i < users.length)
    {
        if (users[i]!="");
        {
            var op=new Option(users[i],users[i]);
            $get("lstMembers").options[$get("lstMembers").options.length]= op;
        }
        i+=1;
    }
    
    //$get("lstMembers").value=$get("txt").value+result;
    //$get("txt").doScroll();
}
AdminPublic.prototype.errorCallback=function(result)
{
	alert("An error occurred while invoking the remote method: " 
	+ result);
}
AdminPublic.prototype.openPrivate=function(name){
    
}
AdminPublic.prototype.openWindow=function(url,windowName){
    var win=window.open(url, windowName, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=450,height=270')
    return win;
}

var APublic=new AdminPublic("APublic");		